package org.testis.wirelesssecuritymodule.core;

import org.testis.data.AtofModuleConstants;
import org.testis.logging.AtofLogLevel;
import org.testis.logging.LogSenderService;
import org.testis.utils.StringUtils;
import org.testis.utils.TerminalApplicationExecutor;
import org.testis.wirelesssecuritymodule.data.WirelessSecurityModuleDataObject;



/**
 *
 * @author nurhat
 */
public class WpaSupplicantProvider extends WirelessConnectionSupplier
{

    TerminalApplicationExecutor m_termApp;

    public WpaSupplicantProvider(WirelessSecurityModuleDataObject dObj)
    {
        super(dObj);
    }


    @Override
    public void afterExecute()
    {
        if (m_termApp != null) {

            //send wpa_supplicant output
            String output = m_termApp.getOutputString();
            String errorOutput = m_termApp.getErrorString();
            LogSenderService.putFileMessageOnQueue("WpaSupplicantProvider",
                                                   AtofLogLevel.DEBUG,
                                                   "\n Exit Code:\n" +
                m_termApp.getExitValue() +
                "\nOutput:\n" + output +
                "\n Error:\n" + errorOutput,
                                                   getDobj().getMessageId());
        }
    }


    public void run()
    {
        try {
            String command = getCommandToRun();
            System.out.println("Command:" + command);
            m_termApp = new TerminalApplicationExecutor(command);
            m_termApp.runExec();
        } catch (InterruptedException ex) {
            throw new WirelessSecurityModuleException(
                WirelessSecurityModuleExceptionType.UNEXPLAINED,
                "Execution of WpaSupplicantProvider:Wireless connection interrupted due to timeout..." +
                ex.getMessage());
        } catch (Exception ex) {
            throw new WirelessSecurityModuleException(
                WirelessSecurityModuleExceptionType.UNEXPLAINED,
                "Execution of WpaSupplicantProvider:Wireless connection run failed..." +
                ex.getMessage());
        } finally {
            m_termApp.endExec();
        }
    }


    

    private String getCommandToRun() throws   Exception
    {
        String conName = getDobj().getParamInterface();
        String profileFolder = AtofModuleConstants.ATOF_USER_TEMP_DIRECTORY + "/" +
            StringUtils.removeSpaces(conName);
        String profileFile = profileFolder + "/" + getDobj().getWpaSsid() +
            ".conf";
        String wpaDriver = getDobj().getWpaDriver();
        StringBuffer cmd = new StringBuffer();
        cmd.append("wpa_supplicant");
        if (wpaDriver != null) {
            if (!wpaDriver.trim().equals("")) {
                cmd.append(" -D " + wpaDriver);
            }
        }

        if (getNicGuid() != null&& !getNicGuid().isEmpty()) {
            cmd.append(" -i " + getNicGuid());
        }
         else
            throw  new Exception(getDobj().getParamInterface() +" interface not found");
        cmd.append(" -c " + profileFile);
        cmd.append(" -dd ");
        return cmd.toString();
    }
}
